package com.inplaceedit.client.activity.main.contact;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.inplaceedit.client.ui.SaveCancelButtonBar;
import com.inplaceedit.client.ui.contact.ContactNameEditor;
import com.inplaceedit.client.ui.contact.ContactNameEditorView;
import com.inplaceedit.client.ui.contact.ContactSexSelectorView;
import com.inplaceedit.client.ui.phone.PhoneEditor;
import com.inplaceedit.client.ui.phone.PhoneListEditorView;
import com.inplaceedit.shared.dto.ContactDto;
import com.inplaceedit.shared.dto.PhoneDto;

public class ContactNewView extends Composite implements
		ContactNewActivityImpl.View {

	interface Binder extends UiBinder<Widget, ContactNewView> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	ContactNameEditorView nameEditor;
	@UiField
	DateBox birthdayEditor;
	@UiField
	ContactSexSelectorView sexSelector;
	@UiField
	PhoneListEditorView phoneListEditor;
	@UiField
	SaveCancelButtonBar commandBar;

	private Handler handler;

	public ContactNewView() {
		initWidget(uiBinder.createAndBindUi(this));

		birthdayEditor.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT)));

		commandBar.getSaveCommand().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				onSaveCommandClick(event);
			}
		});
		commandBar.getCancelCommand().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				onCancelCommandClick(event);
			}
		});
	}

	@Override
	public void enableSave(final boolean enabled) {
		commandBar.setSaveEnabled(enabled);
	}

	@Override
	public LeafValueEditor<Date> getBirthdayEditor() {
		return birthdayEditor.asEditor();
	}

	@Override
	public ContactNameEditor getNameEditor() {
		return nameEditor;
	}

	@Override
	public ListEditor<PhoneDto, PhoneEditor> getPhoneListEditor() {
		return phoneListEditor.asEditor();
	}

	@Override
	public LeafValueEditor<ContactDto.Sex> getSexSelector() {
		return sexSelector.asEditor();
	}

	@Override
	public void setHandler(final Handler handler) {
		this.handler = handler;
	}

	void onCancelCommandClick(final ClickEvent event) {
		if (handler != null) {
			handler.onCancel();
		}
	}

	void onSaveCommandClick(final ClickEvent event) {
		if (handler != null) {
			handler.onSave();
		}
	}
}
